##### Step 1: load data and packages #####



library(tidyverse)
library(lubridate)
library(data.table)
library(haven)
library(fixest)
library(magrittr)
library(ggplot2)
library(MASS)
library(plm)
library(lmtest)
library(broom)
library(xtable)

base_dir <- "..." # Manually change this home directory
data_dir <- paste0(base_dir, "data/")

options(scipen=999)

## function to more easily specify controls in regression formulas
fixest_form <- function(
  y,
  x, 
  fe="0",
  x_re = FALSE,
  df = NULL
  ) {

  if (x_re & !is.null(df)) {
    x <- x %>% map(~ grep(., colnames(df), value=T)) %>% reduce(c)
  }
  
  xpart <- paste(x, collapse = " + ")
  fepart <- paste(fe, collapse = " + ")



  y %>% 
    paste(paste(xpart, fepart, sep="|"), sep=" ~ ") %>%
    as.formula

}

chpos <- readRDS(paste0(data_dir, "cable_zip.rds"))
load(paste0(data_dir, "zip_demographics_2010.RData"))

census2010 %>% setDT
chpos <- census2010[chpos, on = .(zipcode)]

chpos[,zip:=as.integer(zipcode)]

zip_fips <- fread(paste0(data_dir, "DMA-zip.csv")) %>% 
  .[,.(fips=FIPS, zipcode = str_pad(ZIPCODE, pad="0", width=5), dmacode=`DMA CODE`)] %>% 
  unique(on="zipcode")
zip_fips %<>% mutate(
  fips = str_pad(fips, pad="0", width=5)
)
zip_fips %<>% mutate(
  state = as.factor(str_sub(fips, 1, 2))
)
n_distinct(zip_fips$state)
chpos %<>% left_join(zip_fips, by="zipcode")
chpos <- chpos[position_year==2009]

donations_summ <- readRDS(paste0(data_dir, "donations-congressional-summ.rds"))
donations_summ %<>% rename("zip"="contributor.zipcode.cleaned.5")
donations_summ$zip %<>% as.integer()
chpos %<>% left_join(donations_summ, by="zip")

dv.list <- c(
  
  "amount_tea_party_house_newdonors",
  "amount_tea_party_house_olddonors",
  
  "n_tea_party_house_newdonors",
  "n_tea_party_house_olddonors",
  
  "amount_tea_party_house_tercile_all1",
  "amount_tea_party_house_tercile_all2",
  "amount_tea_party_house_tercile_all3",
  
  "n_tea_party_house_tercile_all1",
  "n_tea_party_house_tercile_all2",
  "n_tea_party_house_tercile_all3",
  
  "amount_tea_party_house_tercile_r1",
  "amount_tea_party_house_tercile_r2",
  "amount_tea_party_house_tercile_r3",
  
  "n_tea_party_house_tercile_r1",
  "n_tea_party_house_tercile_r2",
  "n_tea_party_house_tercile_r3"
  
)
for(var in dv.list) {
  text.command <- paste0("chpos[is.na(", var, ") | ", var, "<0, ", var, ":=0]")
  eval(parse(text=text.command))
}

chpos %<>% mutate(
  state = case_when(
    zip == "29707" ~ "45",  # South Carolina
    zip == "33472" ~ "12",  # Florida
    zip == "33578" ~ "12",  # Florida
    zip == "33579" ~ "12",  # Florida
    zip == "60642" ~ "17",  # Illinois
    zip == "73012" ~ "40",  # Oklahoma
    zip == "73025" ~ "40",  # Oklahoma
    zip == "78633" ~ "48",  # Texas
    zip == "78665" ~ "48",  # Texas
    zip == "80023" ~ "08",  # Colorado
    zip == "84081" ~ "49",  # Utah
    zip == "85138" ~ "04",  # Arizona
    zip == "85142" ~ "04",  # Arizona
    zip == "85143" ~ "04",  # Arizona
    zip == "85286" ~ "04",  # Arizona
    zip == "85295" ~ "04",  # Arizona
    zip == "85298" ~ "04",  # Arizona
    zip == "85392" ~ "04",  # Arizona
    zip == "86315" ~ "04",  # Arizona
    zip == "94505" ~ "06",  # California
    TRUE ~ state  # keep existing value if zip doesn't match
  )
)





##### Step 2: main regressions #####



# setup regressions
base_controls <- c(
  "pos_lin_msnbc",
  "i(chan_config)",
  "num_channels",
  "num_bc")

demog_controls <- c(
                    "pct_black", "pct_asian", "pct_other", "pct_hisp",
                    "pct_male",
                    "pct_age_10s", "pct_age_20s", "pct_age_30s", "pct_age_40s", "pct_age_50s", "pct_age_60s", "pct_age_70s", "pct_age_80s",
                    "i(income_dec)",
                    "pct_hsgrad", "pct_somecollege", "pct_bach", "pct_postgrad",
                    "pct_suburban", "pct_urban"
)

dv.list <- c(

  "amount_tea_party_house_newdonors",
  "amount_tea_party_house_olddonors",

  "n_tea_party_house_newdonors",
  "n_tea_party_house_olddonors",
  
  "amount_tea_party_house_tercile_all1",
  "amount_tea_party_house_tercile_all2",
  "amount_tea_party_house_tercile_all3",

  "n_tea_party_house_tercile_all1",
  "n_tea_party_house_tercile_all2",
  "n_tea_party_house_tercile_all3",

  "amount_tea_party_house_tercile_r1",
  "amount_tea_party_house_tercile_r2",
  "amount_tea_party_house_tercile_r3",

  "n_tea_party_house_tercile_r1",
  "n_tea_party_house_tercile_r2",
  "n_tea_party_house_tercile_r3"

)

for(dv in dv.list) {
  
  print(paste0("-----", dv, "-----"))
  
  f_base <- fixest_form(y=dv, x=c("pos_lin_fnc", base_controls), fe="0")
  f_controls <- fixest_form(y=dv, x=c("pos_lin_fnc", base_controls, demog_controls), fe="0")
  f_controls_statefe <- fixest_form(y=dv, x=c("pos_lin_fnc", base_controls, demog_controls), fe="state")
  
  m_base <- feols(f_base, data=chpos[position_year == 2009], cluster="stid")
  print(m_base)
  m_controls <- feols(f_controls, data=chpos[position_year == 2009], cluster="stid")
  print(m_controls)
  m_controls_statefe <- feols(f_controls_statefe, data=chpos[position_year == 2009], cluster="stid")
  print(m_controls_statefe)
  
  assign(paste0(dv,"_m_base"), m_base)
  assign(paste0(dv,"_m_controls"), m_controls)
  assign(paste0(dv,"_m_controls_statefe"), m_controls_statefe)

  
}

# Print Figure G.7.1 -----

resid_y <- resid(feols(fixest_form(y="amount_tea_party_house_olddonors", 
                                   x=c(
                                     # "pos_lin_fnc",
                                     base_controls, demog_controls), fe="state"),
                       chpos[position_year == 2009], cluster="stid"))
resid_x <- resid(feols(fixest_form(y="pos_lin_fnc", 
                                   x=c(
                                     # "pos_lin_fnc",
                                     base_controls, demog_controls), fe="state"),
                       chpos[position_year == 2009], cluster="stid"))
X = "FNC Cable Channel Position"
Y = "Contribution Amount to Tea Party Candidates\nFrom Existing Contributors"
print(ggplot(data.frame(resid_y, resid_x), aes(x = resid_x, y = resid_y)) +
  geom_point(alpha = 0.1) +
  geom_smooth(method = "lm", se = FALSE, color = "blue") +
  labs(
    x = paste0("Residualized ", X),
    y = paste0("Residualized Log-Transformed\n", Y)
  ) +
  theme_light() +
  scale_y_continuous(trans = "log1p") +
  theme(    axis.text = element_text(size=12),
            axis.title = element_text(size=15),
            legend.title = element_text(size=15),
            legend.text = element_text(size=12)) )



# Print Figure G.7.2 -----

resid_y <- resid(feols(fixest_form(y="amount_tea_party_house_newdonors", 
                                   x=c(
                                     # "pos_lin_fnc",
                                     base_controls, demog_controls), fe="state"),
                       chpos[position_year == 2009], cluster="stid"))
resid_x <- resid(feols(fixest_form(y="pos_lin_fnc", 
                                   x=c(
                                     # "pos_lin_fnc",
                                     base_controls, demog_controls), fe="state"),
                       chpos[position_year == 2009], cluster="stid"))
X = "FNC Cable Channel Position"
Y = "Contribution Amount to Tea Party Candidates\nFrom New Contributors"
print(ggplot(data.frame(resid_y, resid_x), aes(x = resid_x, y = resid_y)) +
  geom_point(alpha = 0.1) +
  geom_smooth(method = "lm", se = FALSE, color = "blue") +
  labs(
    x = paste0("Residualized ", X),
    y = paste0("Residualized Log-Transformed\n", Y)
  ) +
  theme_light() +
  scale_y_continuous(trans = "log1p") +
  theme(    axis.text = element_text(size=12),
            axis.title = element_text(size=15),
            legend.title = element_text(size=15),
            legend.text = element_text(size=12)) )



# Print Figure G.10.1 -----

resid_y <- resid(feols(fixest_form(y="amount_tea_party_house_tercile_all1", 
                                   x=c(
                                     # "pos_lin_fnc",
                                     base_controls, demog_controls), fe="state"),
                       chpos[position_year == 2009], cluster="stid"))
resid_x <- resid(feols(fixest_form(y="pos_lin_fnc", 
                                   x=c(
                                     # "pos_lin_fnc",
                                     base_controls, demog_controls), fe="state"),
                       chpos[position_year == 2009], cluster="stid"))
X = "FNC Cable Channel Position"
Y = "Contribution Amount to Tea Party Candidates\nFrom 1st Tercile of (All) Donor Ideology"
ggplot(data.frame(resid_y, resid_x), aes(x = resid_x, y = resid_y)) +
  geom_point(alpha = 0.1) +
  geom_smooth(method = "lm", se = FALSE, color = "blue") +
  labs(
    x = paste0("Residualized ", X),
    y = paste0("Residualized Log-Transformed\n", Y)
  ) +
  theme_light() +
  scale_y_continuous(trans = "log1p") +
  theme(    axis.text = element_text(size=12),
            axis.title = element_text(size=15),
            legend.title = element_text(size=15),
            legend.text = element_text(size=12))  %>% print()



# Print Figure G.10.2 -----

resid_y <- resid(feols(fixest_form(y="amount_tea_party_house_tercile_all2", 
                                   x=c(
                                     # "pos_lin_fnc",
                                     base_controls, demog_controls), fe="state"),
                       chpos[position_year == 2009], cluster="stid"))
resid_x <- resid(feols(fixest_form(y="pos_lin_fnc", 
                                   x=c(
                                     # "pos_lin_fnc",
                                     base_controls, demog_controls), fe="state"),
                       chpos[position_year == 2009], cluster="stid"))
X = "FNC Cable Channel Position"
Y = "Contribution Amount to Tea Party Candidates\nFrom 2nd Tercile of (All) Donor Ideology"
ggplot(data.frame(resid_y, resid_x), aes(x = resid_x, y = resid_y)) +
  geom_point(alpha = 0.1) +
  geom_smooth(method = "lm", se = FALSE, color = "blue") +
  labs(
    x = paste0("Residualized ", X),
    y = paste0("Residualized Log-Transformed\n", Y)
  ) +
  theme_light() +
  scale_y_continuous(trans = "log1p") +
  theme(    axis.text = element_text(size=12),
            axis.title = element_text(size=15),
            legend.title = element_text(size=15),
            legend.text = element_text(size=12)) %>% print()



# Print Figure G.10.3 -----

resid_y <- resid(feols(fixest_form(y="amount_tea_party_house_tercile_all3", 
                                   x=c(
                                     # "pos_lin_fnc",
                                     base_controls, demog_controls), fe="state"),
                       chpos[position_year == 2009], cluster="stid"))
resid_x <- resid(feols(fixest_form(y="pos_lin_fnc", 
                                   x=c(
                                     # "pos_lin_fnc",
                                     base_controls, demog_controls), fe="state"),
                       chpos[position_year == 2009], cluster="stid"))
X = "FNC Cable Channel Position"
Y = "Contribution Amount to Tea Party Candidates\nFrom 3rd Tercile of (All) Donor Ideology"
ggplot(data.frame(resid_y, resid_x), aes(x = resid_x, y = resid_y)) +
  geom_point(alpha = 0.1) +
  geom_smooth(method = "lm", se = FALSE, color = "blue") +
  labs(
    x = paste0("Residualized ", X),
    y = paste0("Residualized Log-Transformed\n", Y)
  ) +
  theme_light() +
  scale_y_continuous(trans = "log1p") +
  theme(    axis.text = element_text(size=12),
            axis.title = element_text(size=15),
            legend.title = element_text(size=15),
            legend.text = element_text(size=12)) %>% print()



# Print Figure G.12.1 -----

resid_y <- resid(feols(fixest_form(y="amount_tea_party_house_tercile_r1", 
                                   x=c(
                                     # "pos_lin_fnc",
                                     base_controls, demog_controls), fe="state"),
                       chpos[position_year == 2009], cluster="stid"))
resid_x <- resid(feols(fixest_form(y="pos_lin_fnc", 
                                   x=c(
                                     # "pos_lin_fnc",
                                     base_controls, demog_controls), fe="state"),
                       chpos[position_year == 2009], cluster="stid"))
X = "FNC Cable Channel Position"
Y = "Contribution Amount to Tea Party Candidates\nFrom 1st Tercile of (Republican) Donor Ideology"
ggplot(data.frame(resid_y, resid_x), aes(x = resid_x, y = resid_y)) +
  geom_point(alpha = 0.1) +
  geom_smooth(method = "lm", se = FALSE, color = "blue") +
  labs(
    x = paste0("Residualized ", X),
    y = paste0("Residualized Log-Transformed\n", Y)
  ) +
  theme_light() +
  scale_y_continuous(trans = "log1p") +
  theme(    axis.text = element_text(size=12),
            axis.title = element_text(size=15),
            legend.title = element_text(size=15),
            legend.text = element_text(size=12)) %>% print()



# Print Figure G.12.2 -----

resid_y <- resid(feols(fixest_form(y="amount_tea_party_house_tercile_r2", 
                                   x=c(
                                     # "pos_lin_fnc",
                                     base_controls, demog_controls), fe="state"),
                       chpos[position_year == 2009], cluster="stid"))
resid_x <- resid(feols(fixest_form(y="pos_lin_fnc", 
                                   x=c(
                                     # "pos_lin_fnc",
                                     base_controls, demog_controls), fe="state"),
                       chpos[position_year == 2009], cluster="stid"))
X = "FNC Cable Channel Position"
Y = "Contribution Amount to Tea Party Candidates\nFrom 2nd Tercile of (Republican) Donor Ideology"
ggplot(data.frame(resid_y, resid_x), aes(x = resid_x, y = resid_y)) +
  geom_point(alpha = 0.1) +
  geom_smooth(method = "lm", se = FALSE, color = "blue") +
  labs(
    x = paste0("Residualized ", X),
    y = paste0("Residualized Log-Transformed\n", Y)
  ) +
  theme_light() +
  scale_y_continuous(trans = "log1p") +
  theme(    axis.text = element_text(size=12),
            axis.title = element_text(size=15),
            legend.title = element_text(size=15),
            legend.text = element_text(size=12)) %>% print()



# Print Figure G.12.3 -----

resid_y <- resid(feols(fixest_form(y="amount_tea_party_house_tercile_r3", 
                                   x=c(
                                     # "pos_lin_fnc",
                                     base_controls, demog_controls), fe="state"),
                       chpos[position_year == 2009], cluster="stid"))
resid_x <- resid(feols(fixest_form(y="pos_lin_fnc", 
                                   x=c(
                                     # "pos_lin_fnc",
                                     base_controls, demog_controls), fe="state"),
                       chpos[position_year == 2009], cluster="stid"))
X = "FNC Cable Channel Position"
Y = "Contribution Amount to Tea Party Candidates\nFrom 3rd Tercile of (Republican) Donor Ideology"
ggplot(data.frame(resid_y, resid_x), aes(x = resid_x, y = resid_y)) +
  geom_point(alpha = 0.1) +
  geom_smooth(method = "lm", se = FALSE, color = "blue") +
  labs(
    x = paste0("Residualized ", X),
    y = paste0("Residualized Log-Transformed\n", Y)
  ) +
  theme_light() +
  scale_y_continuous(trans = "log1p") +
  theme(    axis.text = element_text(size=12),
            axis.title = element_text(size=15),
            legend.title = element_text(size=15),
            legend.text = element_text(size=12)) %>% print()




setFixest_etable(fitstat = ~ n + r2)

tablestyle  = style.tex(main="aer",
                        fixef.suffix = " FEs",
                        fixef.where ="var",
                        fixef.title = "",
                        stats.title = "\\midrule",
                        tablefoot=F,
                        yesNo="\\checkmark")

setFixest_dict(c(zip="Zip",
                 state="State",
                 pos_lin_fnc = "FNC Channel Pos.",
                 pos_lin_msnbc = "MSNBC Channel Pos.",
                 amount_tea_party="Contributions to Tea Party Candidates ($)",
                 amount_non_tea_party="Contributions to non-Tea Party R Candidates ($)",
                 n_donors_tea_party="No. Contributors to Tea Party Candidates",
                 n_donors_non_tea_party="No. Contributors to Non-Tea Party R Candidates",
                 amount_dem = "Contributions to Democratic Candidates ($)",
                 n_dem = "No. Contributors to Democratic Candidates",
                 amount_tea_party_house_fnc = "Contributions to Tea Party Candidates ($)",
                 amount_tea_party_house_nofnc = "Contributions to Tea Party Candidates ($)",
                 n_tea_party_house_fnc = "No. Contributors to Tea Party Candidates",
                 n_tea_party_house_nofnc = "No. Contributors to Tea Party Candidates",
                 amount_tea_party_house_newdonors = "New Contributors",
                 amount_tea_party_house_olddonors = "Existing Contributors",
                 n_tea_party_house_newdonors = "New Contributors",
                 n_tea_party_house_olddonors = "Existing Contributors",
                 amount_tea_party_house_tercile_all1 = "Donor Ideology $(−1, −0.342)$",
                 amount_tea_party_house_tercile_all2 = "Donor Ideology $(−0.342, 0.289)$",
                 amount_tea_party_house_tercile_all3 = "Donor Ideology $(0.289, 1)$",
                 n_tea_party_house_tercile_all1 = "Donor Ideology $(−1, −0.342)$",
                 n_tea_party_house_tercile_all2 = "Donor Ideology $(−0.342, 0.289)$",
                 n_tea_party_house_tercile_all3 = "Donor Ideology $(0.289, 1)$",
                 amount_tea_party_house_tercile_r1 = "Donor Ideology $(−0.691, 0.367)$",
                 amount_tea_party_house_tercile_r2 = "Donor Ideology $(0.367, 0.456)$",
                 amount_tea_party_house_tercile_r3 = "Donor Ideology $(0.456, 0.965)$",
                 n_tea_party_house_tercile_r1 = "Donor Ideology $(−0.691, 0.367)$",
                 n_tea_party_house_tercile_r2 = "Donor Ideology $(0.367, 0.456)$",
                 n_tea_party_house_tercile_r3 = "Donor Ideology $(0.456, 0.965)$"
),
reset=TRUE
)



if(length(amount_tea_party_house_olddonors_m_base$obs_selection$obsRemoved)==0) {
  mean_y1 <- mean(as.integer(chpos$amount_tea_party_house_olddonors[chpos$position_year == 2009]), na.rm = TRUE)
} else {
  mean_y1 <- mean(chpos$amount_tea_party_house_olddonors[setdiff(1:nrow(chpos), -amount_tea_party_house_olddonors_m_base$obs_selection$obsRemoved)], na.rm = TRUE)
}
mean_y2 <- mean(chpos$amount_tea_party_house_olddonors[setdiff(1:nrow(chpos), -amount_tea_party_house_olddonors_m_controls$obs_selection$obsRemoved)], na.rm = TRUE)
mean_y3 <- mean(chpos$amount_tea_party_house_olddonors[setdiff(1:nrow(chpos), -amount_tea_party_house_olddonors_m_controls_statefe$obs_selection$obsRemoved)], na.rm = TRUE)
if(length(amount_tea_party_house_newdonors_m_base$obs_selection$obsRemoved)==0) {
  mean_y4 <- mean(as.integer(chpos$amount_tea_party_house_newdonors[chpos$position_year == 2009]), na.rm = TRUE)
} else {
  mean_y4 <- mean(chpos$amount_tea_party_house_newdonors[setdiff(1:nrow(chpos), -amount_tea_party_house_newdonors_m_base$obs_selection$obsRemoved)], na.rm = TRUE)
}
mean_y5 <- mean(chpos$amount_tea_party_house_newdonors[setdiff(1:nrow(chpos), -amount_tea_party_house_newdonors_m_controls$obs_selection$obsRemoved)], na.rm = TRUE)
mean_y6 <- mean(chpos$amount_tea_party_house_newdonors[setdiff(1:nrow(chpos), -amount_tea_party_house_newdonors_m_controls_statefe$obs_selection$obsRemoved)], na.rm = TRUE)

# Print Table 6 -----

print(etable(
       amount_tea_party_house_olddonors_m_base, amount_tea_party_house_olddonors_m_controls, amount_tea_party_house_olddonors_m_controls_statefe,
       amount_tea_party_house_newdonors_m_base, amount_tea_party_house_newdonors_m_controls, amount_tea_party_house_newdonors_m_controls_statefe,
       extralines = list("__Mean of DV" = c(sprintf("%.3f", mean_y1), sprintf("%.3f", mean_y2), sprintf("%.3f", mean_y3), sprintf("%.3f", mean_y4), sprintf("%.3f", mean_y5), sprintf("%.3f", mean_y6))),
       # file = paste0(table_dir, "contributions_amount_old_vs_new_donors.tex"),
       title = "Zip-Code Itemized Contributions (In Dollars) to Tea Party Candidates Among Existing vs. New Contributors",
       label = paste0("table:contributions_amount_oldvsnew"),
       cluster=~stid,
       drop = "(Constant)",
       group=list("\\midrule Cable system controls"=c("chan", base_controls),
                  "Demographic controls"=c("income", demog_controls)),
       digits=3,
       digits.stats=2,
       style.tex=tablestyle,
       replace = TRUE

))



if(length(n_tea_party_house_olddonors_m_base$obs_selection$obsRemoved)==0) {
  mean_y1 <- mean(as.integer(chpos$n_tea_party_house_olddonors[chpos$position_year == 2009]), na.rm = TRUE)
} else {
  mean_y1 <- mean(chpos$n_tea_party_house_olddonors[setdiff(1:nrow(chpos), -n_tea_party_house_olddonors_m_base$obs_selection$obsRemoved)], na.rm = TRUE)
}
mean_y2 <- mean(chpos$n_tea_party_house_olddonors[setdiff(1:nrow(chpos), -n_tea_party_house_olddonors_m_controls$obs_selection$obsRemoved)], na.rm = TRUE)
mean_y3 <- mean(chpos$n_tea_party_house_olddonors[setdiff(1:nrow(chpos), -n_tea_party_house_olddonors_m_controls_statefe$obs_selection$obsRemoved)], na.rm = TRUE)
if(length(n_tea_party_house_newdonors_m_base$obs_selection$obsRemoved)==0) {
  mean_y4 <- mean(as.integer(chpos$n_tea_party_house_newdonors[chpos$position_year == 2009]), na.rm = TRUE)
} else {
  mean_y4 <- mean(chpos$n_tea_party_house_newdonors[setdiff(1:nrow(chpos), -n_tea_party_house_newdonors_m_base$obs_selection$obsRemoved)], na.rm = TRUE)
}
mean_y5 <- mean(chpos$n_tea_party_house_newdonors[setdiff(1:nrow(chpos), -n_tea_party_house_newdonors_m_controls$obs_selection$obsRemoved)], na.rm = TRUE)
mean_y6 <- mean(chpos$n_tea_party_house_newdonors[setdiff(1:nrow(chpos), -n_tea_party_house_newdonors_m_controls_statefe$obs_selection$obsRemoved)], na.rm = TRUE)

# Print Table G.8.1 -----

print(etable(
  n_tea_party_house_olddonors_m_base, n_tea_party_house_olddonors_m_controls, n_tea_party_house_olddonors_m_controls_statefe,
  n_tea_party_house_newdonors_m_base, n_tea_party_house_newdonors_m_controls, n_tea_party_house_newdonors_m_controls_statefe,
  extralines = list("__Mean of DV" = c(sprintf("%.3f", mean_y1), sprintf("%.3f", mean_y2), sprintf("%.3f", mean_y3), sprintf("%.3f", mean_y4), sprintf("%.3f", mean_y5), sprintf("%.3f", mean_y6))),
 # file = paste0(table_dir, "contributions_n_old_vs_new_donors.tex"),
  title = "Zip-Code Itemized Contributions (By Total Number of Contributors) to Tea Party Candidates Among Existing vs. New Contributors",
  label = paste0("table:contributions_n_oldvsnew"),
  cluster=~stid,
  drop = "(Constant)",
  group=list("\\midrule Cable system controls"=c("chan", base_controls),
             "Demographic controls"=c("income", demog_controls)),
  digits=3,
  digits.stats=2,
  style.tex=tablestyle,
  replace = TRUE
  
))



if(length(amount_tea_party_house_tercile_all1_m_base$obs_selection$obsRemoved)==0) {
  mean_y1 <- mean(as.integer(chpos$amount_tea_party_house_tercile_all1[chpos$position_year == 2009]), na.rm = TRUE)
} else {
  mean_y1 <- mean(chpos$amount_tea_party_house_tercile_all1[setdiff(1:nrow(chpos), -amount_tea_party_house_tercile_all1_m_base$obs_selection$obsRemoved)], na.rm = TRUE)
}
mean_y2 <- mean(chpos$amount_tea_party_house_tercile_all1[setdiff(1:nrow(chpos), -amount_tea_party_house_tercile_all1_m_controls$obs_selection$obsRemoved)], na.rm = TRUE)
mean_y3 <- mean(chpos$amount_tea_party_house_tercile_all1[setdiff(1:nrow(chpos), -amount_tea_party_house_tercile_all1_m_controls_statefe$obs_selection$obsRemoved)], na.rm = TRUE)
if(length(amount_tea_party_house_tercile_all2_m_base$obs_selection$obsRemoved)==0) {
  mean_y4 <- mean(as.integer(chpos$amount_tea_party_house_tercile_all2[chpos$position_year == 2009]), na.rm = TRUE)
} else {
  mean_y4 <- mean(chpos$amount_tea_party_house_tercile_all2[setdiff(1:nrow(chpos), -amount_tea_party_house_tercile_all2_m_base$obs_selection$obsRemoved)], na.rm = TRUE)
}
mean_y5 <- mean(chpos$amount_tea_party_house_tercile_all2[setdiff(1:nrow(chpos), -amount_tea_party_house_tercile_all2_m_controls$obs_selection$obsRemoved)], na.rm = TRUE)
mean_y6 <- mean(chpos$amount_tea_party_house_tercile_all2[setdiff(1:nrow(chpos), -amount_tea_party_house_tercile_all2_m_controls_statefe$obs_selection$obsRemoved)], na.rm = TRUE)
if(length(amount_tea_party_house_tercile_all3_m_base$obs_selection$obsRemoved)==0) {
  mean_y7 <- mean(as.integer(chpos$amount_tea_party_house_tercile_all3[chpos$position_year == 2009]), na.rm = TRUE)
} else {
  mean_y7 <- mean(chpos$amount_tea_party_house_tercile_all3[setdiff(1:nrow(chpos), -amount_tea_party_house_tercile_all3_m_base$obs_selection$obsRemoved)], na.rm = TRUE)
}
mean_y8 <- mean(chpos$amount_tea_party_house_tercile_all3[setdiff(1:nrow(chpos), -amount_tea_party_house_tercile_all3_m_controls$obs_selection$obsRemoved)], na.rm = TRUE)
mean_y9 <- mean(chpos$amount_tea_party_house_tercile_all3[setdiff(1:nrow(chpos), -amount_tea_party_house_tercile_all3_m_controls_statefe$obs_selection$obsRemoved)], na.rm = TRUE)

# Print Table 7 -----

print(etable(
  amount_tea_party_house_tercile_all1_m_base, amount_tea_party_house_tercile_all1_m_controls, amount_tea_party_house_tercile_all1_m_controls_statefe,
  amount_tea_party_house_tercile_all2_m_base, amount_tea_party_house_tercile_all2_m_controls, amount_tea_party_house_tercile_all2_m_controls_statefe,
  amount_tea_party_house_tercile_all3_m_base, amount_tea_party_house_tercile_all3_m_controls, amount_tea_party_house_tercile_all3_m_controls_statefe,
  extralines = list("__Mean of DV" = c(sprintf("%.3f", mean_y1), sprintf("%.3f", mean_y2), sprintf("%.3f", mean_y3), sprintf("%.3f", mean_y4), sprintf("%.3f", mean_y5), sprintf("%.3f", mean_y6), sprintf("%.3f", mean_y7), sprintf("%.3f", mean_y8), sprintf("%.3f", mean_y9))),
   # file = paste0(table_dir, "contributions_amount_tercile_all.tex"),
  title = "Zip-Code Itemized Contributions (In Dollars) to Tea Party Candidates Across Existing Contributors",
  label = paste0("table:contributions_amount_tercile_all"),
  cluster=~stid,
  drop = "(Constant)",
  group=list("\\midrule Cable system controls"=c("chan", base_controls),
             "Demographic controls"=c("income", demog_controls)),
  digits=3,
  digits.stats=2,
  style.tex=tablestyle,
  replace = TRUE,
  placement = "H"
))



if(length(n_tea_party_house_tercile_all1_m_base$obs_selection$obsRemoved)==0) {
  mean_y1 <- mean(as.integer(chpos$n_tea_party_house_tercile_all1[chpos$position_year == 2009]), na.rm = TRUE)
} else {
  mean_y1 <- mean(chpos$n_tea_party_house_tercile_all1[setdiff(1:nrow(chpos), -n_tea_party_house_tercile_all1_m_base$obs_selection$obsRemoved)], na.rm = TRUE)
}
mean_y2 <- mean(chpos$n_tea_party_house_tercile_all1[setdiff(1:nrow(chpos), -n_tea_party_house_tercile_all1_m_controls$obs_selection$obsRemoved)], na.rm = TRUE)
mean_y3 <- mean(chpos$n_tea_party_house_tercile_all1[setdiff(1:nrow(chpos), -n_tea_party_house_tercile_all1_m_controls_statefe$obs_selection$obsRemoved)], na.rm = TRUE)
if(length(n_tea_party_house_tercile_all2_m_base$obs_selection$obsRemoved)==0) {
  mean_y4 <- mean(as.integer(chpos$n_tea_party_house_tercile_all2[chpos$position_year == 2009]), na.rm = TRUE)
} else {
  mean_y4 <- mean(chpos$n_tea_party_house_tercile_all2[setdiff(1:nrow(chpos), -n_tea_party_house_tercile_all2_m_base$obs_selection$obsRemoved)], na.rm = TRUE)
}
mean_y5 <- mean(chpos$n_tea_party_house_tercile_all2[setdiff(1:nrow(chpos), -n_tea_party_house_tercile_all2_m_controls$obs_selection$obsRemoved)], na.rm = TRUE)
mean_y6 <- mean(chpos$n_tea_party_house_tercile_all2[setdiff(1:nrow(chpos), -n_tea_party_house_tercile_all2_m_controls_statefe$obs_selection$obsRemoved)], na.rm = TRUE)
if(length(n_tea_party_house_tercile_all3_m_base$obs_selection$obsRemoved)==0) {
  mean_y7 <- mean(as.integer(chpos$n_tea_party_house_tercile_all3[chpos$position_year == 2009]), na.rm = TRUE)
} else {
  mean_y7 <- mean(chpos$n_tea_party_house_tercile_all3[setdiff(1:nrow(chpos), -n_tea_party_house_tercile_all3_m_base$obs_selection$obsRemoved)], na.rm = TRUE)
}
mean_y8 <- mean(chpos$n_tea_party_house_tercile_all3[setdiff(1:nrow(chpos), -n_tea_party_house_tercile_all3_m_controls$obs_selection$obsRemoved)], na.rm = TRUE)
mean_y9 <- mean(chpos$n_tea_party_house_tercile_all3[setdiff(1:nrow(chpos), -n_tea_party_house_tercile_all3_m_controls_statefe$obs_selection$obsRemoved)], na.rm = TRUE)

# Print Table G.13.1 -----

print(etable(
  n_tea_party_house_tercile_all1_m_base, n_tea_party_house_tercile_all1_m_controls, n_tea_party_house_tercile_all1_m_controls_statefe,
  n_tea_party_house_tercile_all2_m_base, n_tea_party_house_tercile_all2_m_controls, n_tea_party_house_tercile_all2_m_controls_statefe,
  n_tea_party_house_tercile_all3_m_base, n_tea_party_house_tercile_all3_m_controls, n_tea_party_house_tercile_all3_m_controls_statefe,
  extralines = list("__Mean of DV" = c(sprintf("%.3f", mean_y1), sprintf("%.3f", mean_y2), sprintf("%.3f", mean_y3), sprintf("%.3f", mean_y4), sprintf("%.3f", mean_y5), sprintf("%.3f", mean_y6), sprintf("%.3f", mean_y7), sprintf("%.3f", mean_y8), sprintf("%.3f", mean_y9))),
  # file = paste0(table_dir, "contributions_n_tercile_all.tex"),
  title = "Zip-Code Itemized Contributions (By Total Number of Contributors) to Tea Party Candidates Across Existing Contributors",
  label = paste0("table:contributions_n_tercile_all"),
  cluster=~stid,
  drop = "(Constant)",
  group=list("\\midrule Cable system controls"=c("chan", base_controls),
             "Demographic controls"=c("income", demog_controls)),
  digits=3,
  digits.stats=2,
  style.tex=tablestyle,
  replace = TRUE,
  placement="H"
))



if(length(amount_tea_party_house_tercile_r1_m_base$obs_selection$obsRemoved)==0) {
  mean_y1 <- mean(as.integer(chpos$amount_tea_party_house_tercile_r1[chpos$position_year == 2009]), na.rm = TRUE)
} else {
  mean_y1 <- mean(chpos$amount_tea_party_house_tercile_r1[setdiff(1:nrow(chpos), -amount_tea_party_house_tercile_r1_m_base$obs_selection$obsRemoved)], na.rm = TRUE)
}
mean_y2 <- mean(chpos$amount_tea_party_house_tercile_r1[setdiff(1:nrow(chpos), -amount_tea_party_house_tercile_r1_m_controls$obs_selection$obsRemoved)], na.rm = TRUE)
mean_y3 <- mean(chpos$amount_tea_party_house_tercile_r1[setdiff(1:nrow(chpos), -amount_tea_party_house_tercile_r1_m_controls_statefe$obs_selection$obsRemoved)], na.rm = TRUE)
if(length(amount_tea_party_house_tercile_r2_m_base$obs_selection$obsRemoved)==0) {
  mean_y4 <- mean(as.integer(chpos$amount_tea_party_house_tercile_r2[chpos$position_year == 2009]), na.rm = TRUE)
} else {
  mean_y4 <- mean(chpos$amount_tea_party_house_tercile_r2[setdiff(1:nrow(chpos), -amount_tea_party_house_tercile_r2_m_base$obs_selection$obsRemoved)], na.rm = TRUE)
}
mean_y5 <- mean(chpos$amount_tea_party_house_tercile_r2[setdiff(1:nrow(chpos), -amount_tea_party_house_tercile_r2_m_controls$obs_selection$obsRemoved)], na.rm = TRUE)
mean_y6 <- mean(chpos$amount_tea_party_house_tercile_r2[setdiff(1:nrow(chpos), -amount_tea_party_house_tercile_r2_m_controls_statefe$obs_selection$obsRemoved)], na.rm = TRUE)
if(length(amount_tea_party_house_tercile_r3_m_base$obs_selection$obsRemoved)==0) {
  mean_y7 <- mean(as.integer(chpos$amount_tea_party_house_tercile_r3[chpos$position_year == 2009]), na.rm = TRUE)
} else {
  mean_y7 <- mean(chpos$amount_tea_party_house_tercile_r3[setdiff(1:nrow(chpos), -amount_tea_party_house_tercile_r3_m_base$obs_selection$obsRemoved)], na.rm = TRUE)
}
mean_y8 <- mean(chpos$amount_tea_party_house_tercile_r3[setdiff(1:nrow(chpos), -amount_tea_party_house_tercile_r3_m_controls$obs_selection$obsRemoved)], na.rm = TRUE)
mean_y9 <- mean(chpos$amount_tea_party_house_tercile_r3[setdiff(1:nrow(chpos), -amount_tea_party_house_tercile_r3_m_controls_statefe$obs_selection$obsRemoved)], na.rm = TRUE)

# Print Table 8 -----

print(etable(
  amount_tea_party_house_tercile_r1_m_base, amount_tea_party_house_tercile_r1_m_controls, amount_tea_party_house_tercile_r1_m_controls_statefe,
  amount_tea_party_house_tercile_r2_m_base, amount_tea_party_house_tercile_r2_m_controls, amount_tea_party_house_tercile_r2_m_controls_statefe,
  amount_tea_party_house_tercile_r3_m_base, amount_tea_party_house_tercile_r3_m_controls, amount_tea_party_house_tercile_r3_m_controls_statefe,
  extralines = list("__Mean of DV" = c(sprintf("%.3f", mean_y1), sprintf("%.3f", mean_y2), sprintf("%.3f", mean_y3), sprintf("%.3f", mean_y4), sprintf("%.3f", mean_y5), sprintf("%.3f", mean_y6), sprintf("%.3f", mean_y7), sprintf("%.3f", mean_y8), sprintf("%.3f", mean_y9))),
  # file = paste0(table_dir, "contributions_amount_tercile_r.tex"),
  title = "Zip-Code Itemized Contributions (In Dollars) to Tea Party Candidates Across Existing Republican Contributors",
  label = paste0("table:contributions_amount_tercile_r"),
  cluster=~stid,
  drop = "(Constant)",
  group=list("\\midrule Cable system controls"=c("chan", base_controls),
             "Demographic controls"=c("income", demog_controls)),
  digits=3,
  digits.stats=2,
  style.tex=tablestyle,
  replace = TRUE,
  placement = "H"
))



if(length(n_tea_party_house_tercile_r1_m_base$obs_selection$obsRemoved)==0) {
  mean_y1 <- mean(as.integer(chpos$n_tea_party_house_tercile_r1[chpos$position_year == 2009]), na.rm = TRUE)
} else {
  mean_y1 <- mean(chpos$n_tea_party_house_tercile_r1[setdiff(1:nrow(chpos), -n_tea_party_house_tercile_r1_m_base$obs_selection$obsRemoved)], na.rm = TRUE)
}
mean_y2 <- mean(chpos$n_tea_party_house_tercile_r1[setdiff(1:nrow(chpos), -n_tea_party_house_tercile_r1_m_controls$obs_selection$obsRemoved)], na.rm = TRUE)
mean_y3 <- mean(chpos$n_tea_party_house_tercile_r1[setdiff(1:nrow(chpos), -n_tea_party_house_tercile_r1_m_controls_statefe$obs_selection$obsRemoved)], na.rm = TRUE)
if(length(n_tea_party_house_tercile_r2_m_base$obs_selection$obsRemoved)==0) {
  mean_y4 <- mean(as.integer(chpos$n_tea_party_house_tercile_r2[chpos$position_year == 2009]), na.rm = TRUE)
} else {
  mean_y4 <- mean(chpos$n_tea_party_house_tercile_r2[setdiff(1:nrow(chpos), -n_tea_party_house_tercile_r2_m_base$obs_selection$obsRemoved)], na.rm = TRUE)
}
mean_y5 <- mean(chpos$n_tea_party_house_tercile_r2[setdiff(1:nrow(chpos), -n_tea_party_house_tercile_r2_m_controls$obs_selection$obsRemoved)], na.rm = TRUE)
mean_y6 <- mean(chpos$n_tea_party_house_tercile_r2[setdiff(1:nrow(chpos), -n_tea_party_house_tercile_r2_m_controls_statefe$obs_selection$obsRemoved)], na.rm = TRUE)
if(length(n_tea_party_house_tercile_r3_m_base$obs_selection$obsRemoved)==0) {
  mean_y7 <- mean(as.integer(chpos$n_tea_party_house_tercile_r3[chpos$position_year == 2009]), na.rm = TRUE)
} else {
  mean_y7 <- mean(chpos$n_tea_party_house_tercile_r3[setdiff(1:nrow(chpos), -n_tea_party_house_tercile_r3_m_base$obs_selection$obsRemoved)], na.rm = TRUE)
}
mean_y8 <- mean(chpos$n_tea_party_house_tercile_r3[setdiff(1:nrow(chpos), -n_tea_party_house_tercile_r3_m_controls$obs_selection$obsRemoved)], na.rm = TRUE)
mean_y9 <- mean(chpos$n_tea_party_house_tercile_r3[setdiff(1:nrow(chpos), -n_tea_party_house_tercile_r3_m_controls_statefe$obs_selection$obsRemoved)], na.rm = TRUE)

# Print Table G.13.2 -----

print(etable(
  n_tea_party_house_tercile_r1_m_base, n_tea_party_house_tercile_r1_m_controls, n_tea_party_house_tercile_r1_m_controls_statefe,
  n_tea_party_house_tercile_r2_m_base, n_tea_party_house_tercile_r2_m_controls, n_tea_party_house_tercile_r2_m_controls_statefe,
  n_tea_party_house_tercile_r3_m_base, n_tea_party_house_tercile_r3_m_controls, n_tea_party_house_tercile_r3_m_controls_statefe,
  extralines = list("__Mean of DV" = c(sprintf("%.3f", mean_y1), sprintf("%.3f", mean_y2), sprintf("%.3f", mean_y3), sprintf("%.3f", mean_y4), sprintf("%.3f", mean_y5), sprintf("%.3f", mean_y6), sprintf("%.3f", mean_y7), sprintf("%.3f", mean_y8), sprintf("%.3f", mean_y9))),
  # file = paste0(table_dir, "contributions_n_tercile_r.tex"),
  title = "Zip-Code Itemized Contributions (By Total Number of Contributors) to Tea Party Candidates Across Existing Republican Contributors",
  label = paste0("table:contributions_n_tercile_r"),
  cluster=~stid,
  drop = "(Constant)",
  group=list("\\midrule Cable system controls"=c("chan", base_controls),
             "Demographic controls"=c("income", demog_controls)),
  digits=3,
  digits.stats=2,
  style.tex=tablestyle,
  replace = TRUE,
  placement = "H"
))





##### Step 3: sensitivity analysis #####



# setup regressions
base_controls <- c(
  "pos_lin_msnbc",
  "i(chan_config)",
  "num_channels",
  "num_bc")

demog_controls <- c(
  "pct_black", "pct_asian", "pct_other", "pct_hisp",
  "pct_male",
  "pct_age_10s", "pct_age_20s", "pct_age_30s", "pct_age_40s", "pct_age_50s", "pct_age_60s", "pct_age_70s", "pct_age_80s",
  "i(income_dec)",
  "pct_hsgrad", "pct_somecollege", "pct_bach", "pct_postgrad",
  "pct_suburban", "pct_urban"
)

dv.list <- c(
  

  "amount_tea_party_house_olddonors",
  "amount_tea_party_house_newdonors",
  
  "amount_tea_party_house_tercile_all1",
  "amount_tea_party_house_tercile_all2",
  "amount_tea_party_house_tercile_all3",
  
  "amount_tea_party_house_tercile_r1",
  "amount_tea_party_house_tercile_r2",
  "amount_tea_party_house_tercile_r3"
  
)

for(dv in dv.list) {
  
  print(paste0("-----", dv, "-----"))
  
  assign(paste0(dv, "_f_reduced"), fixest_form(y=dv, x=c("pos_lin_fnc", base_controls), fe="state"))
  
  full_model <- get(paste0(dv, "_m_controls_statefe"))
  rows.to.remove <- -full_model$obs_selection$obsRemoved
  assign(paste0(dv, "_m_reduced"), feols(get(paste0(dv, "_f_reduced")), 
                                         data=chpos[setdiff(1:nrow(chpos), rows.to.remove), ], cluster="stid"))
  
  # calculate oster ratio
  reduced_model <- get(paste0(dv, "_m_reduced"))
  coef_reduced <- reduced_model$coefficients["pos_lin_fnc"]
  r2_reduced <- as.numeric(fitstat(reduced_model, "wr2"))
  coef_full <- full_model$coefficients["pos_lin_fnc"]
  r2_full <- as.numeric(fitstat(full_model, "wr2"))
  oster_ratio <- coef_full*(r2_full-r2_reduced)/((coef_reduced - coef_full)*(min(1, 1.3*r2_full)-r2_full))
  assign(paste0(dv, "_oster_ratio"), oster_ratio)
  print(oster_ratio)
  # setwd(misc_dir)
  # write(round(get(paste0(dv, "_oster_ratio")), digit=1), paste0(dv, "_oster_ratio.tex"))
  
  # calculate oster bounds
  coef_bound <- coef_full - (coef_reduced-coef_full)*(min(1, 1.3*r2_full)-r2_full)/(r2_full-r2_reduced)
  print(coef_bound)
  if(coef_bound >= coef_full) {
    if(round(coef_full, digits=1)==0) {
      oster_bound <- paste0("[", as.character(signif(coef_full, digits=3)), ", ", as.character(signif(coef_bound, digit=3)), "]")
    } else{
      oster_bound <- paste0("[", as.character(round(coef_full, digits=1)), ", ", as.character(round(coef_bound, digit=1)), "]")
    }
    print(oster_bound)
    # write(oster_bound, paste0(dv, "_oster_bound.tex"))
  } else {
    if(round(coef_full, digits=1)==0) {
      oster_bound <- paste0("[", as.character(signif(coef_bound, digits=3)), ", ", as.character(signif(coef_full, digit=3)), "]")
    } else{
      oster_bound <- paste0("[", as.character(round(coef_bound, digits=1)), ", ", as.character(round(coef_full, digit=1)), "]")
    }
    print(oster_bound)
    # write(oster_bound, paste0(dv, "_oster_bound.tex"))
  }

  ## setup for tables
  setFixest_dict(c(zip="Zip",
                   state="State",
                   pos_lin_fnc = "FNC Channel Pos.",
                   pos_lin_msnbc = "MSNBC Channel Pos.",
                   amount_tea_party="Tea Party Candidates",
                   amount_non_tea_party="Other Rep. Candidates",
                   n_donors_tea_party="Tea Party Candidates",
                   n_donors_non_tea_party="Other Rep. Candidates",
                   amount_tea_party_house_newdonors = "New Donors",
                   amount_tea_party_house_olddonors = "Existing Donors",
                   amount_tea_party_house_tercile_all1 = "1st Tercile of All Donor Ideology",
                   amount_tea_party_house_tercile_all2 = "2nd Tercile of All Donor Ideology",
                   amount_tea_party_house_tercile_all3 = "3rd Tercile of All Donor Ideology",
                   amount_tea_party_house_tercile_r1 = "1st Tercile of Republican Donor Ideology",
                   amount_tea_party_house_tercile_r2 = "2nd Tercile of Republican Donor Ideology",
                   amount_tea_party_house_tercile_r3 = "3rd Tercile of Republican Donor Ideology",
                   amount_dem = "Dollar Amounts",
                   n_dem = "Number of Itemized Contributors",
                   stid = "Cable System"
  ),
  reset=TRUE
  )
  
  setFixest_etable(fitstat = ~ n + war2) # adjusted within R2
  
  tablestyle  = style.tex(main="aer",
                          fixef.suffix = " FEs",
                          fixef.where ="var",
                          fixef.title = "",
                          stats.title = "\\midrule",
                          tablefoot=F,
                          yesNo="\\checkmark")
  
  mean_y1 <- mean(chpos[[dv]][setdiff(1:nrow(chpos), -full_model$obs_selection$obsRemoved)], na.rm = TRUE)
  mean_y2 <- mean(chpos[[dv]][setdiff(1:nrow(chpos), -full_model$obs_selection$obsRemoved)], na.rm = TRUE)
  
  file = paste0("contributions_", dv, "_oster_ratio.tex")
  title = case_when(
    dv=="amount_tea_party_house_newdonors" ~ "Itemized Contributions In Dollars From New Contributors",
    dv=="amount_tea_party_house_olddonors" ~ "Itemized Contributions In Dollasrs From Existing Contributors",
    dv=="amount_tea_party_house_tercile_all1" ~ "Itemized Contributions In Dollars From The 1st Tercile of (All) Donor Ideology",
    dv=="amount_tea_party_house_tercile_all2" ~ "Itemized Contributions In Dollars From The 2nd Tercile of (All) Donor Ideology",
    dv=="amount_tea_party_house_tercile_all3" ~ "Itemized Contributions In Dollars From The 3rd Tercile of (All) Donor Ideology",
    dv=="amount_tea_party_house_tercile_r1" ~ "Itemized Contributions In Dollars From The 1st Tercile of (Republican) Donor Ideology",
    dv=="amount_tea_party_house_tercile_r2" ~ "Itemized Contributions In Dollars From The 2nd Tercile of (Republican) Donor Ideology",
    dv=="amount_tea_party_house_tercile_r3" ~ "Itemized Contributions In Dollars From The 3rd Tercile of (Republican) Donor Ideology",
    TRUE ~ NA_character_
  )
  title = paste0(title, " (for Oster Ratio Calculation)")
  label = paste0("table:contributions-", str_replace_all(dv, pattern="_", replacement="-"), "-oster-ratio")
  
  # Print Tables G.6.1, G.6.2, G.9.1, G.9.2, G.9.3, G.11.1, G.11.2, G.11.3 -----
  
  print(etable(reduced_model, full_model,
         extralines = list("__Mean of DV" = c(sprintf("%.3f", mean_y1), sprintf("%.3f", mean_y2))),
         # file = file,
         title = title,
         label = label,
         cluster=~stid,
         drop = "(Constant)",
         group=list("\\midrule Cable system controls"=c("chan", base_controls),
                    "Demographic controls"=c("income", demog_controls)),
         digits=3,
         digits.stats=2,
         style.tex=tablestyle,
         replace = T,
         depvar = F,
         placement = "H"
  ))
  
}



